home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / wizard / PreviewCellRenderer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-20  |  3.7 KB  |  118 lines

  1. package asp.wizard;
  2.  
  3. import com.sun.java.swing.JLabel;
  4. import com.sun.java.swing.JTable;
  5. import com.sun.java.swing.table.TableCellRenderer;
  6. import java.awt.Color;
  7. import java.awt.Component;
  8. import java.awt.Dimension;
  9. import java.awt.Font;
  10. import java.awt.FontMetrics;
  11. import java.awt.Graphics;
  12. import java.awt.Point;
  13.  
  14. public class PreviewCellRenderer extends JLabel implements TableCellRenderer {
  15.    public static final int MODE_PAINT = 0;
  16.    public static final int MODE_MEASURE = 1;
  17.    private static final int SIDE_MARGIN = 3;
  18.    private static final int VERT_MARGIN = 2;
  19.    private String _currText;
  20.    private int _row;
  21.    private int _column;
  22.    private JTable _table;
  23.    private TextAttribute[] _textAttributes = new TextAttribute[]{new TextAttribute(this), new TextAttribute(this)};
  24.    private int _maxWidth;
  25.    private int _maxHeight;
  26.    private boolean _isPainting = true;
  27.  
  28.    public void paint(Graphics g) {
  29.       Point loc = ((Component)this).getLocation();
  30.       Dimension dim = ((Component)this).getSize();
  31.       TextAttribute attr = null;
  32.       if (this._row == 0) {
  33.          attr = this._textAttributes[0];
  34.       } else {
  35.          attr = this._textAttributes[1];
  36.       }
  37.  
  38.       g.setFont(attr._font);
  39.       g.setColor(attr._color);
  40.       FontMetrics fm = g.getFontMetrics();
  41.       int text_height = fm.getAscent() + fm.getDescent();
  42.       int y = (dim.height - text_height >> 1) + fm.getAscent();
  43.       int width = fm.stringWidth(this._currText);
  44.       if (this._isPainting) {
  45.          g.drawString(this._currText, 3, y);
  46.          if (attr._underline) {
  47.             g.translate(0, 2);
  48.             g.drawLine(3, y, width, y);
  49.             g.translate(0, 0);
  50.          }
  51.       } else {
  52.          if (this._row == 0 && this._column == 0) {
  53.             this._maxHeight = this._maxWidth = 0;
  54.          }
  55.  
  56.          int total_height = text_height + 8;
  57.          if (total_height > this._maxHeight) {
  58.             this._maxHeight = total_height;
  59.          }
  60.  
  61.          int total_width = width + 12;
  62.          if (total_width > this._maxWidth) {
  63.             this._maxWidth = total_width;
  64.          }
  65.       }
  66.  
  67.    }
  68.  
  69.    private final void setTextAttributes(int index, Font font, boolean underline, Color color) {
  70.       this._textAttributes[index]._font = font;
  71.       this._textAttributes[index]._underline = underline;
  72.       this._textAttributes[index]._color = color;
  73.    }
  74.  
  75.    public final void setFirstRowTextAttributes(Font font, boolean underline, Color color) {
  76.       this.setTextAttributes(0, font, underline, color);
  77.    }
  78.  
  79.    public final void setOtherRowTextAttributes(Font font, boolean underline, Color color) {
  80.       this.setTextAttributes(1, font, underline, color);
  81.    }
  82.  
  83.    public Dimension getMaximumCellSize(Dimension placeHolder) {
  84.       if (placeHolder == null) {
  85.          placeHolder = new Dimension();
  86.       }
  87.  
  88.       placeHolder.width = this._maxWidth;
  89.       placeHolder.height = this._maxHeight;
  90.       return placeHolder;
  91.    }
  92.  
  93.    public void setMode(int mode) {
  94.       switch (mode) {
  95.          case 0:
  96.             this._isPainting = true;
  97.             break;
  98.          case 1:
  99.             this._isPainting = false;
  100.             break;
  101.          default:
  102.             this._isPainting = true;
  103.       }
  104.  
  105.    }
  106.  
  107.    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  108.       if (value != null) {
  109.          this._currText = value.toString();
  110.       }
  111.  
  112.       this._row = row;
  113.       this._column = column;
  114.       this._table = table;
  115.       return this;
  116.    }
  117. }
  118.